package org.example.resourceplatform.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

@Entity
@Table(name = "document_rating",
        uniqueConstraints = {
                @UniqueConstraint(name = "uk_rating_user_doc", columnNames = {"user_id", "doc_id"})
        },
        indexes = {
                @Index(name = "idx_rating_user", columnList = "user_id"),
                @Index(name = "idx_rating_doc", columnList = "doc_id"),
                @Index(name = "idx_rating_value", columnList = "rating_value"),
                @Index(name = "idx_rating_time", columnList = "rating_time")
        })
@Getter
@Setter
@NoArgsConstructor
public class DocumentRating {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "rating_id")
    private Integer ratingId;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "doc_id", nullable = false)
    private Document document;

    @Min(1)
    @Max(5)
    @Column(name = "rating_value", nullable = false)
    private Integer ratingValue;

    @CreationTimestamp
    @Column(name = "rating_time", nullable = false, updatable = false)
    private LocalDateTime ratingTime;

    @Column(name = "comment", length = 200)
    private String comment;

    public DocumentRating(User user, Document document, Integer ratingValue, String comment) {
        this.user = user;
        this.document = document;
        this.ratingValue = ratingValue;
        this.comment = comment;
    }

    @Override
    public String toString() {
        return "DocumentRating{" +
                "ratingId=" + ratingId +
                ", userId=" + (user != null ? user.getUserId() : null) +
                ", documentId=" + (document != null ? document.getDocId() : null) +
                ", ratingValue=" + ratingValue +
                '}';
    }
}